﻿

local moduleName = "ManaBar"



----------------------------
--      Localization      --
----------------------------

local L = {
	[moduleName] = "Mana Bar",

	["Texture"] = true,
	["The texture used for the mana bar."] = true,

	["Height"] = true,
	["Height of the bar."] = true,

	["Rage"] = true,
	["Show rage bars."] = true,

	["Energy"] = true,
	["Show energy bars."] = true,

	["Mana"] = true,
	["Show mana bars."] = true
}
if GetLocale() == "zhCN" then
	L[moduleName] = "法力条"

	L["Texture"] = "贴图"
	L["The texture used for the mana bar."] = "法力条的贴图"

	L["Height"] = "高"
	L["Height of the bar."] = "法力条的高度"

	L["Rage"] = "怒气条"
	L["Show rage bars."] = "显示怒气条"

	L["Energy"] = "能量条"
	L["Show energy bars."] = "显示能量条"

	L["Mana"] = "法力条"
	L["Show mana bars."] = "显示法力条"
elseif GetLocale() == "zhTW" then
	L[moduleName] = "法力條"

	L["Texture"] = "貼圖"
	L["The texture used for the mana bar."] = "法力條的貼圖"

	L["Height"] = "高"
	L["Height of the bar."] = "法力條的高度"

	L["Rage"] = "怒氣條"
	L["Show rage bars."] = "顯示怒氣條"

	L["Energy"] = "能量條"
	L["Show energy bars."] = "顯示能量條"

	L["Mana"] = "法力條"
	L["Show mana bars."] = "顯示法力條"
end


----------------------------------
--      Local Declaration      --
----------------------------------

local SharedMedia     = LibStub("LibSharedMedia-3.0")
local SharedMediaType = SharedMedia.MediaType and SharedMedia.MediaType.STATUSBAR or "statusbar"
local tbl = SharedMedia:List(SharedMediaType)
local UnitManaMax = UnitManaMax
local UnitMana = UnitMana
local UnitPowerType = UnitPowerType


----------------------------------
--      Module Declaration      --
----------------------------------

local plugin = BunchOfBars:NewModule(moduleName)

plugin.revision = tonumber(("$Revision: 40 $"):match("%d+"))

plugin.options = {
	name = L[moduleName],
	args = {
		texture = {
			type     = 'select',
			name     = L["Texture"],
			desc     = L["The texture used for the mana bar."],
			get      = "GetSetTexture",
			set      = "GetSetTexture",
			values = tbl
		},
		height = {
			type  = "range",
			name  = L["Height"],
			desc  = L["Height of the bar."],
			min   = 1,
		        max   = 30,
		        step  = 1,
		        get   = "GetSetHeight",
		        set   = "GetSetHeight"
		},
		mana = {
			type  = "toggle",
			name  = L["Mana"],
			desc  = L["Show mana bars."],
		        get   = "GetSetMana",
		        set   = "GetSetMana",
			order = 1
		},
		rage = {
			type  = "toggle",
			name  = L["Rage"],
			desc  = L["Show rage bars."],
		        get   = "GetSetRage",
		        set   = "GetSetRage",
			order = 2
		},
		energy = {
			type  = "toggle",
			name  = L["Energy"],
			desc  = L["Show energy bars."],
		        get   = "GetSetEnergy",
		        set   = "GetSetEnergy",
			order = 3
		}
	}
}

plugin.defaultDB = {
	profile = {
		position = 1,
		texture   = "bar1",
		height    = 4,
		rage   = false,
		energy = false,
		mana   = true
	}
}

----------------------------------
--      Module Functions        --
----------------------------------

function plugin:OnEnable()
	self:RegisterEvents()
end

function plugin:OnCreate(frame)
	local texture = SharedMedia:Fetch(SharedMediaType, self.db.profile.texture)

	local bar = CreateFrame("StatusBar", nil, frame.parts["HealthBar"])
	bar:SetStatusBarTexture(texture)
	bar:SetHeight(self.db.profile.height)
	bar:SetMinMaxValues(0, 1)
	bar:ClearAllPoints()
	bar:SetPoint("BOTTOMLEFT", frame.parts["HealthBar"], "BOTTOMLEFT", 0, 0)
	bar:SetPoint("BOTTOMRIGHT", frame.parts["HealthBar"], "BOTTOMRIGHT", 0, 0)
	bar:Hide()

	local back = bar:CreateTexture(nil, "BACKGROUND")
	back:SetTexture(texture)
	back:SetVertexColor(0, 0, 0, 0.4)
	back:ClearAllPoints()
	back:SetAllPoints(bar)
	back:Show()

	bar.back = back

	return bar
end

function plugin:OnUpdate(frame)
if not UnitExists(frame.unit) then return end
	local bar = frame.parts[moduleName]
	local unit = frame.unit
	local t = UnitPowerType(unit)
	local p

	if not UnitIsConnected(unit) then
		t = 4
	end
	if t == 0 and self.db.profile.mana then
		bar:SetStatusBarColor(48/255, 113/255, 191/255)
	elseif t == 1 and self.db.profile.rage then
		bar:SetStatusBarColor(226/255, 45/255, 75/255)
	elseif t == 3 and self.db.profile.energy then
		bar:SetStatusBarColor(1, 220/255, 25/255)
	else
		bar:Hide()
		return
	end
	
	p = UnitMana(unit) / UnitManaMax(unit)
	bar:SetValue(p)
	bar:Show()
end


function plugin:RegisterEvents()
	self:UnregisterAllEvents()
	local e = { }

	if self.db.profile.mana then
		table.insert(e, "UNIT_MANA")
		table.insert(e, "UNIT_MAXMANA")
	end

	if self.db.profile.rage then
		table.insert(e, "UNIT_RAGE")
		table.insert(e, "UNIT_MAXRAGE")
	end

	if self.db.profile.energy then
		table.insert(e, "UNIT_ENERGY")
		table.insert(e, "UNIT_MAXENERGY")
	end

	if table.getn(e) > 0 then
		table.insert(e, "UNIT_DISPLAYPOWER")
--		self:RegisterBucketEvent(e, 0.5, "UpdateUnits")
		for _,ev in pairs(e) do
			self:RegisterEvent(ev, "UpdateUnit")
		end
	end
end

----------------------------------
--      Option Handlers         --
----------------------------------

function plugin:GetSetTexture(_, v)
	if type(v) == "nil" then
		for i, k in pairs(tbl) do
			if k == self.db.profile.texture then
				return i
			end
		end
	end

	if self.db.profile.texture ~= tbl[v] then
		self.db.profile.texture = tbl[v]

		self:UpdateAllWith(function(frame)
			frame.parts[moduleName]:SetStatusBarTexture(SharedMedia:Fetch(SharedMediaType, self.db.profile.texture))
			frame.parts[moduleName].back:SetTexture(SharedMedia:Fetch(SharedMediaType, self.db.profile.texture))
		end)
	end
end


function plugin:GetSetHeight(_, v)
	if type(v) == "nil" then return self.db.profile.height end

	if self.db.profile.height ~= v then
		self.db.profile.height = v

		self:UpdateAllWith(function(frame)
			frame.parts[moduleName]:SetHeight(self.db.profile.height)
		end)
		self:UpdateAll()
	end
end

function plugin:GetSetRage(_, v)
	if type(v) == "nil" then return self.db.profile.rage end

	if self.db.profile.rage ~= v then
		self.db.profile.rage = v

		self:UpdateAll()
		self:RegisterEvents()
	end
end

function plugin:GetSetEnergy(_, v)
	if type(v) == "nil" then return self.db.profile.energy end

	if self.db.profile.energy ~= v then
		self.db.profile.energy = v

		self:UpdateAll()
		self:RegisterEvents()
	end
end

function plugin:GetSetMana(_, v)
	if type(v) == "nil" then return self.db.profile.mana end

	if self.db.profile.mana ~= v then
		self.db.profile.mana = v

		self:UpdateAll()
		self:RegisterEvents()
	end
end
